package cn.usian.jyzx.product.controller;

import cn.usian.jyzx.model.dto.ProductItemVo;
import cn.usian.jyzx.model.dto.ProductSkuDto;
import cn.usian.jyzx.model.entity.product.Category;
import cn.usian.jyzx.model.entity.product.ProductSku;
import cn.usian.jyzx.model.vo.common.Result;
import cn.usian.jyzx.model.vo.common.ResultCodeEnum;
import cn.usian.jyzx.model.vo.h5.IndexVo;
import cn.usian.jyzx.product.service.CategoryService;
import cn.usian.jyzx.product.service.ProductSkuService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @TODO:
 * @Author:baimu
 * @Date:2024 10 30 13 52
 **/

@RestController

public class ProductController {

    @Autowired
    CategoryService categoryService;

    @Autowired
    ProductSkuService productSkuService;

    ///api/product/index



    @GetMapping("/api/product/index")
    public Result index(){
        //一级分类数据
        List<Category> oneCategoryList = categoryService.findByParentId(0L);

        List<ProductSku> bySaleNum = productSkuService.findBySaleNum();

        IndexVo indexVo = new IndexVo();
        indexVo.setCategoryList(oneCategoryList);
        indexVo.setProductSkuList(bySaleNum);

        return Result.build(indexVo, ResultCodeEnum.SUCCESS);
    }

    /**
     * redis 缓存
     * set  key -value
     * key：allCategoryList       value：List
     * get  key
     *
     * set
     * get
     *
     *
     * 1：从redis查询，有--》return
     * 2：没有--》mysql---》set redis
     *
     * @return
     */
    @GetMapping("/api/product/category/findCategoryTree")
    public Result category(){
        List<Category> allCategory = categoryService.findAllCategory2();
        return Result.build(allCategory, ResultCodeEnum.SUCCESS);
    }


    /**
     * GET: 接受参数是不需求加 @RequestBody ,对象里面的属性必须和前台url后面拼接的参数保持一致
     * POST：接受参数需要加 @RequestBody，{},
     * @RequestBody:将json 字符串转成对象
     * @param page，
     * @param limit
     * @param productSkuDto
     * @return
     */
    @GetMapping("/api/product/{page}/{limit}")
    public Result<PageInfo<ProductSku>> list(@PathVariable("page") Integer page,
                                             @PathVariable("limit") Integer limit,
                                            ProductSkuDto productSkuDto){

        PageInfo<ProductSku> productSkuPageInfo = productSkuService.findByPage(page, limit, productSkuDto);

        return Result.build(productSkuPageInfo, ResultCodeEnum.SUCCESS);



    }



    @GetMapping("/api/product/item/{skuId}")
    public Result itemInfo(@PathVariable Long skuId){

        ProductItemVo productItemVo = productSkuService.itemInfo(skuId);
        return Result.build(productItemVo, ResultCodeEnum.SUCCESS);
    }


    @GetMapping("/api/product/findById/{skuId}")
    public ProductSku findById(@PathVariable("skuId") Long skuId){
        return productSkuService.findById(skuId);
    }

}
